package jDialog;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;

public class RemoveStudents extends JDialog {
	private static final long serialVersionUID = 1L;

	private JTable table;
	private JPanel centerPanel, bottomPanel;
	private String[] classNames;
	private ArrayList<Integer> list = new ArrayList<Integer>();

	public RemoveStudents(JFrame frame, String[] names) {
		super(frame, "Remove Students", ModalityType.DOCUMENT_MODAL);
		classNames = names;
	}

	public ArrayList<Integer> initFrame() {
		JButton cancelBttn = new JButton("Cancel");
		cancelBttn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < table.getRowCount(); i++)
					table.getModel().setValueAt(false, i, 1);
				RemoveStudents.this.dispose();
			}
		});
		JButton removeBttn = new JButton("Remove");
		removeBttn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				for (int i = 0; i < table.getRowCount(); i++)
					if ((boolean) table.getModel().getValueAt(i, 1) == true)
						list.add(i);
				RemoveStudents.this.dispose();
			}
		});
		bottomPanel = new JPanel();
		bottomPanel.add(cancelBttn);
		bottomPanel.add(removeBttn);

		Box verticalBox = Box.createVerticalBox();
		verticalBox.add(centerPanel);
		verticalBox.add(bottomPanel);
		this.add(verticalBox);
		this.pack();
		this.setDialogPos();
		this.setVisible(true);
		return list;
	}

	public void initTable() {
		String[] colNames = { "Student Names", "Remove" };
		Object[][] tableData = new Object[classNames.length][colNames.length];
		for (int i = 0; i < classNames.length; i++)
			tableData[i][0] = classNames[i];
		for (int i = 0; i < classNames.length; i++)
			tableData[i][1] = false;
		table = new JTable(tableData, colNames) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return column != 0;
			}
		};
		table.setAutoCreateRowSorter(true);

		table.getColumnModel().getColumn(0).setPreferredWidth(175);
		table.getColumnModel().getColumn(1).setPreferredWidth(75);
		table.getColumnModel().getColumn(1).setCellEditor(table.getDefaultEditor(Boolean.class));
		table.getColumnModel().getColumn(1).setCellRenderer(table.getDefaultRenderer(Boolean.class));

		JScrollPane scrollTable = new JScrollPane(table);
		scrollTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		table.setFillsViewportHeight(true);
		scrollTable.setPreferredSize(new Dimension(255, 250));
		centerPanel = new JPanel();
		centerPanel.add(scrollTable);
	}

	private void setDialogPos() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((d.getWidth() - this.getWidth()) / 2);
		int y = (int) ((d.getHeight() - this.getHeight()) / 2);
		this.setLocation(x, y);
	}
}
